//
//  BankNameCell.m
//  JiFengDaRen
//
//  Created by zhuchao on 16/7/11.
//  Copyright © 2016年 SH_iOS. All rights reserved.
//

#import "BankNameCell.h"

@implementation BankNameCell
{
    UIView *_bgview;
    NSString *_bankName;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
-(NSArray *)bankNameArr
{
    if (_bankNameArr == nil) {
        _bankNameArr = @[@"中国银行",@"中国农业银行",@"中国工商银行",@"兴业银行",@"光大银行"];
    }
    return _bankNameArr;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self configUI];
    }
    return self;
    
}

-(void)configUI{
    self.nameLabel = [[UILabel alloc]init];
    self.nameLabel.font = KSystemFont(__I6Width(15));
    [self addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(__I6Width(15));
        make.centerY.mas_equalTo(self.mas_centerY);
        make.width.mas_equalTo(__I6Width(100));
        make.height.mas_equalTo(__I6Height(20));
    }];
    
    self.bankNameLabel = [[UILabel alloc]init];
    self.bankNameLabel.font = KSystemFont(__I6Width(15));
    self.bankNameLabel.textColor = [UIColor colorWithWhite:208.f/255.f alpha:1];
    self.bankNameLabel.userInteractionEnabled = YES;
    [self addSubview:self.bankNameLabel];
    [self.bankNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).with.offset(__I6Width(10));
        make.centerY.mas_equalTo(self.mas_centerY);
        make.right.equalTo(self.mas_right).with.offset(__I6Width(-15));
        make.height.mas_equalTo(__I6Height(20));
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectbankName)];
    [self.bankNameLabel addGestureRecognizer:tap];
}
-(void)selectbankName{
    //创建一个消息对象
    NSNotification * notice = [NSNotification notificationWithName:@"收起键盘" object:nil userInfo:nil];
    //发送消息
    [[NSNotificationCenter defaultCenter]postNotification:notice];
    UIPickerView *pinkerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, KScreenHeight - __I6Height(200), KScreenWidth,__I6Height(200))];
    pinkerView.backgroundColor = KBgColor;
    pinkerView.delegate = self;
    pinkerView.dataSource = self;
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    _bgview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
    _bgview.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.3];
    [window addSubview:_bgview];
    [_bgview addSubview:pinkerView];
    UITapGestureRecognizer *dissmissTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dissmissTap:)];
    [_bgview addGestureRecognizer:dissmissTap];
    UIButton *fixedBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth - __I6Width(60)  ,KScreenHeight - __I6Height(190), __I6Width(60),__I6Height(30))];
    [fixedBtn setTitle:@"确定" forState:UIControlStateNormal];
    [fixedBtn setTitleColor:KBaseColor forState:UIControlStateNormal];
    [fixedBtn addTarget:self action:@selector(fixedBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_bgview addSubview:fixedBtn];

    
}
-(void)setLabelText:(NSString *)labelText{
    
    CGFloat labelW  = [SHHelper widthOfString:labelText font:KSystemFont(20) height:__I6Height(20)];
    [self.nameLabel mas_updateConstraints:^(MASConstraintMaker *make){
        make.left.equalTo(self.mas_left).with.offset(__I6Width(15));
        make.top.equalTo(self.mas_top).with.offset(__I6Height(11));
        make.width.mas_equalTo(labelW);
        make.height.mas_equalTo(__I6Height(20));
        
    }];
    self.nameLabel.textColor = [UIColor blackColor];
    self.nameLabel.text = labelText;
    
}
-(void)setPlaceholder:(NSString *)placeholder{
    self.bankNameLabel.text  = placeholder;
}
#pragma mark -- 点击确定按钮
-(void)fixedBtnClicked:(UIButton *)btn{
    
    if (_bankName.length == 0) {
         self.bankNameLabel.text = @"中国银行";
    }
    [Tool setObject:self.bankNameLabel.text forKey:@"bankName"];
    self.bankNameLabel.textColor = [UIColor blackColor];

    [UIView animateWithDuration:0.5 animations:^{
        _bgview.alpha = 0;
    
        
    } completion:^(BOOL finished) {
        [_bgview removeFromSuperview];
    }];

    
    
}
-(void)dissmissTap:(UITapGestureRecognizer *)tap{
    
    [UIView animateWithDuration:0.5 animations:^{
        _bgview.alpha = 0;
        
    } completion:^(BOOL finished) {
        [_bgview removeFromSuperview];
    }];
}
#pragma mark --- UIPickerView DataSouce
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.bankNameArr.count;
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    return self.bankNameArr[row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    _bankName = self.bankNameArr[row];
    self.bankNameLabel.text = self.bankNameArr[row];
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
